#ifndef JOINTSLIDERWIDGET_H
#define JOINTSLIDERWIDGET_H

#include <QWidget>
#include <QSlider>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "customslider.h"
#include "touchbutton.h"

// 自定义按钮类
class DirectionButton : public TouchButton
{
    Q_OBJECT
public:
    explicit DirectionButton(bool isLeft, QWidget *parent = nullptr);
    void setStepSize(int step) { m_step = step; }
    int stepSize() const { return m_step; }

signals:
    void stepClicked(bool isLeft);
    void stepPressed(bool isLeft);
    void stepReleased(bool isLeft);

private slots:
    void onClicked();
    void onPressed();
    void onReleased();

private:
    bool m_isLeft;
    int m_step;
};

class JointSliderWidget : public QWidget
{
    Q_OBJECT
public:
     JointSliderWidget(int w,int h,QWidget *parent = nullptr);
    void init(int nJointId, QString strName,QString strUnit, float min, float max,float value);
    void setCurValue(float value);
    float floatValue() const;
    void setStep(float step);
    int jointId() const { return m_jointId; }

    ~JointSliderWidget()
    {
        // 确保在销毁前断开所有信号连接
        this->disconnect();
        if(m_slider) m_slider->disconnect();
        if(m_leftBtn) m_leftBtn->disconnect();
        if(m_rightBtn) m_rightBtn->disconnect();
    }


signals:
    void start(int jointId, float value);
    void stop(int jointId);
private:
    void setupConnections();
    void initUI();

    DirectionButton *m_leftBtn;
    DirectionButton *m_rightBtn;
    CustomSlider *m_slider;
    QLabel *m_nameLabel;
    QLabel *m_destLabel;
    int m_jointId = -1;
    QString m_strName;
    float m_fCurValue;
    float m_fStep;
    bool m_bInitDest;

    QString m_strUnit;
};

#endif // JOINTSLIDERWIDGET_H
